//index.js

const app = getApp()

Page({
  data: {
    homeNotification: '',
    userNotification: '',
    y: 0,
    y1: 0,
    showMessageBtn: false,
    showManagerTool: true,
    isRefresh: false,
    saveModel: false,
    avatarUrl: './user-unlogin.png',
    userInfo: {},
    logged: false,
    pageCur: '',
    saveId: '',
    isSave: false,
    customBar: app.globalData.CustomBar //colorUI的顶部bar高度是自适应设备的
  },
  getManagerData(){
    wx.cloud.callFunction({
      name: 'getManagerData'
    }).then(res => {
      let managerData = res.result
      if (managerData.homeState && (new Date().getTime() - managerData.homeTextCreateDate) <= Number(managerData.homeConstantTime) * 24 * 3600 * 1000 ){
        this.setData({
          homeNotification: managerData.homeText
        })
      }else{
        this.setData({
          homeNotification: ''
        })
      }
      if (managerData.userState && (new Date().getTime() - managerData.userTextCreateDate) <= Number(managerData.userConstantTime) * 24 * 3600 * 1000 ){
        this.setData({
          userNotification: managerData.userText
        })
      }else{
        this.setData({
          userNotification: ''
        })
      }
    })
  },
  goManagerTool(){
    wx.navigateTo({
      url: '/pages/managerTool/index',
    })
  },
  handleMove(e){
    if (e.detail.y > 0){
      this.setData({
        y: 0
      })
    }
  },
  handleMove1(e) {
    if (e.detail.y > 300) {
      this.setData({
        y1: 300
      })
    }
  },
  
  NavChange(e) {
    if (this.data.pageCur == 'home' && e.currentTarget.dataset.cur == 'home'){
      this.setData({
        isRefresh: true
      })
    }else{
      this.setData({
        isRefresh: false
      })
    }
    // 提示是否保存
    if (this.data.pageCur == 'submit' && e.currentTarget.dataset.cur != 'submit' && this.data.saveModel){
      this.setData({
        showDeleteModal: true,
        savePageCur: e.currentTarget.dataset.cur
      })
    }else{
      this.setData({
        pageCur: e.currentTarget.dataset.cur,
        saveId: ''
      })
    }
  },

  goMessagePage(){
    this.setData({
      showMessageBtn: false
    })
    app.showMessageBtn = false
    wx.navigateTo({
      url: '/pages/message/index',
    })
  },

  notSave(){
    this.setData({
      pageCur: this.data.savePageCur,
      showDeleteModal: false,
      saveModel: false,
      saveId: '',
      isSave: false
    })
  },
  sureSave(){
    this.setData({
      isSave: true,
      // pageCur: this.data.savePageCur,
      showDeleteModal: false,
      saveModel: false,
      saveId: ''
    })
    wx.nextTick(() => {
      this.setData({ 
        pageCur: this.data.savePageCur,
        isSave: false
      }) // 在当前同步流程结束后，下一个时间片执行
    })
  },
  setRefresh(e){
    this.setData({
      isRefresh: e.detail
    })
  },
  showSaveModal(e){
    this.setData({
      saveModel: e.detail
    })
  },
  onLoad: function (options) {
    this.getManagerData()
    this.setData({
      showMessageBtn: app.showMessageBtn,
      showManagerTool: app.user._openid == 'ocGI44105e4QLB6BftO9hUvFcqWE'? true : false
    })
    if (options.component == 'userSetting'){
      this.setData({
        pageCur: 'userSetting'
      })
      wx.showToast({
        title: '修改成功',
        icon: 'success',
        duration: 2000
      })
    } else if (options.component == 'submit'){
      this.setData({
        pageCur: 'submit',
        saveId: options.saveId
      })
    }else{
      this.setData({
        pageCur: 'home'
      })
    }
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          // 已经授权，可以直接调用 getUserInfo 获取头像昵称，不会弹框
          wx.getUserInfo({
            success: res => {
              this.setData({
                avatarUrl: res.userInfo.avatarUrl,
                userInfo: res.userInfo
              })
            }
          })
        }
      }
    })
  },

//授权
  onGetUserInfo: function(e) {
    if (!this.logged && e.detail.userInfo) {
      this.setData({
        logged: true,
        avatarUrl: e.detail.userInfo.avatarUrl,
        userInfo: e.detail.userInfo
      })
    }
  },

  onGetOpenid: function() {
    // 调用云函数获取openId
    wx.cloud.callFunction({
      name: 'login',
      data: {},
      success: res => {
        // console.log('[云函数] [login] user openid: ', res.result.openid)
        app.globalData.openid = res.result.openid
      },
      fail: err => {
        console.error('[云函数] [login] 调用失败', err)
      }
    })
  },
  onShow: function(){
    app.showMessageBtnCallback = (state) => {
      this.setData({
        showMessageBtn: state
      })
    }
  },
  onShareAppMessage: function () {
    return {
      title: '聆听海螺的声音',
      path: '/pages/login/index',
      imageUrl: 'https://636f-conch-voice-test-8c0757-1259463971.tcb.qcloud.la/bg.jpeg?sign=bb3058c75fd3aa64954f44e53051b160&t=1562772880'
    }
  },
  

})
